<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="合同编号" prop="vBillNo">
        <el-input
          v-model="queryParams.vBillNo"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="项目令号" prop="vProjectNo">
        <el-input
          v-model="queryParams.vProjectNo"
          placeholder="请输入项目号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="vSpaName">
        <el-input
          v-model="queryParams.vSpaName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="vSpaModel">
        <el-input
          v-model="queryParams.vSpaModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="品牌" prop="vSpaBrand">
        <el-input
          v-model="queryParams.vSpaBrand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="供应商" prop="supplier">
        <el-input
          v-model="queryParams.supplier"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="申请人" prop="applyBy">
        <el-input
          v-model="queryParams.applyBy"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="postList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同编号" prop="vBillNo" />
      <el-table-column label="项目令号" prop="vProjectNo" />
      <el-table-column label="项目" prop="vProjectDec" />
      <el-table-column label="物料名称" prop="vSpaName" />
      <el-table-column label="规格型号" prop="vSpaModel" />
      <el-table-column label="数量" align="center" prop="nQty" />
      <el-table-column label="货期" prop="nDays" />
      <el-table-column label="申请人" prop="applyBy" />
      <el-table-column label="供应商" prop="supplier" />
      <el-table-column label="品牌" prop="vSpaBrand" />
      <el-table-column label="已入库数量" align="center" prop="nINWHQty" />
      <el-table-column label="备注" width="100px" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleExecute(scope.row)"
            >执行入库</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog
      title="打印入库单"
      :visible.sync="printOpen"
      width="50%"
      append-to-body
    >
      <index2 :products="currentPrintInfo" />
    </el-dialog>

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col>
            <el-form-item label="物料名称" prop="vSpaName">
              <el-input
                v-model="form.vSpaName"
                placeholder="请输入物料名称"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="规格型号" prop="vSpaModel">
              <el-input
                v-model="form.vSpaModel"
                placeholder="请输入规格型号"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="品牌" prop="vSpaBrand">
              <el-input
                v-model="form.vSpaBrand"
                placeholder="请输入品牌"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="数量" prop="nQty">
              <el-input
                v-model="form.nQty"
                placeholder="请输入数量"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="已入库数量" prop="nINWHQty">
              <el-input
                v-model="form.nINWHQty"
                placeholder="请输入已入库数量"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="本次入库数量" prop="nThisINWHQty">
              <el-input
                v-model="form.nThisINWHQty"
                placeholder="请输入本次入库数量"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="仓库" prop="vWHId">
              <el-select
                size="small"
                clearable
                v-model="form.vWHId"
                @change="handleWHEdit()"
              >
                <el-option
                  v-for="dict in whList"
                  :key="dict.id"
                  :label="dict.vWHName"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="库区" prop="vAreaId">
              <el-select
                size="small"
                v-model="form.vAreaId"
                clearable
                @change="handleAreaEdit()"
              >
                <el-option
                  v-for="dict in areaList"
                  :key="dict.id"
                  :label="dict.vWHName"
                  :value="dict.id"
                />
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col>
            <el-form-item label="库位" prop="vLocationId">
              <el-select
                size="small"
                v-model="form.vLocationId"
                clearable
                @change="handleLocaltionEdit()"
              >
                <el-option
                  v-for="dict in localtionList"
                  :key="dict.id"
                  :label="dict.vWHName"
                  :value="dict.id"
                />
              </el-select> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="executeInNew">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  executeIn,
  viewRKBillById,
  listInv0004New,
  executeInNew,
} from "@/api/inv/inv0004";
import { listInv0001 } from "@/api/inv/inv0001";
import index2 from "./index2";
export default {
  name: "inv0004",
  components: { index2 },
  dicts: ["sys_common_status", "inv_arrival_status", "buy_post_type"],
  data() {
    return {
      flag: false,
      // 遮罩层
      loading: true,
      arrivalOpen: false,
      executeOpen: false,
      printOpen: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购邮寄表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vBillNo: null, //合同号
        vProjectNo: null,
        vSpaName: null,
        vSpaModel: null,
        vSpaBrand: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      drawerForm: {},
      detailList: [],
      executeForm: {},
      executeList: [],
      formLabelWidth: "120px",
      whList: [],
      localtionList: [],
      areaList: [],
      areaDisabled: false,
      localtionDisabled: false,
      currentPrintInfo: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购邮寄列表 */
    getList() {
      this.loading = true;
      listInv0004New(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        iD: undefined,
        vSpaName: undefined,
        vSpaModel: undefined,
        vSpaBrand: undefined,
        nQty: undefined,
        nINWHQty: undefined,
        nThisINWHQty: undefined,
        vWHId: undefined,
        vAreaId: undefined,
        vLocationId: undefined,
        supplier: undefined,
        applyBy: undefined,
        vBillNo: undefined,
        vProjectNo: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleExecute(row) {
      this.open = true;
      this.form = row;
      listInv0001({ parentId: "0", whType: "L" }).then((response) => {
        this.whList = response.data;
      });
      this.executeOpen = true;
    },

    cancelDrawer() {
      this.arrivalOpen = false;
      this.queryParams.pageNum = 1;
      this.getList();
    },

    handleWHEdit() {
      console.log(this.form);
      this.form.vAreaId = "";
      this.form.vLocationId = "";
      this.localtionList = [];
      if (this.form.vWHId != "" && this.form.vWHId != "undefined") {
        listInv0001({ parentId: this.form.vWHId, whType: "L" }).then(
          (response) => {
            this.areaList = response.data;
            this.areaDisabled = false;
          }
        );
      } else {
        this.areaList = [];
        this.areaDisabled = true;
        this.localtionDisabled = true;
      }
    },

    handleAreaEdit() {
      this.form.vAreaId = this.form.vAreaId;
      this.form.vLocationId = "";
      this.localtionList = [];
      if (this.form.vAreaId != "" && this.form.vAreaId != "undefined") {
        listInv0001({ parentId: this.form.vAreaId, whType: "L" }).then(
          (response) => {
            this.localtionList = response.data;
            this.localtionDisabled = false;
          }
        );
      } else {
        this.localtionDisabled = true;
      }
    },

    handleLocaltionEdit() {
      if (this.form.vAreaId != "" && this.form.vAreaId != "undefined") {
        listInv0001({ parentId: this.form.vAreaId, whType: "L" }).then(
          (response) => {
            this.localtionList = response.data;
            row.vLocationId = row.vLocationId;
          }
        );
      }
    },

    executeInNew() {
      executeInNew(this.form).then((response) => {
        if (response.code == "200") {
          this.$modal.msgSuccess("操作成功");
          this.open = false;
          this.queryParams.pageNum = 1;
          this.getList();
        }
      });
    },
    handlePrint(row) {
      viewRKBillById(row).then((response) => {
        if (response.code == "200") {
          console.log(response);
          this.currentPrintInfo = response.data;
          this.printOpen = true;
        }
      });
    },
  },
};
</script>
